import { getRouteForm, startScore } from '../../api/manage'
import create from 'dd-store'
import pageStore from '../../stores/globalStore'

create.Page({
  store: pageStore,
  data: {
    mark: [
      { label: '考核结束后自动公开', value: '1' },
      { label: '管理员公示结果', value: '2' }
    ],
    disabled: false,
    disablede: false,
    inputVal: '请选择',
    isShowInputValueModel: false,
    pmGroupName: '',
    user: false,
    group: false,
    pmFormId: '',
    userOrGroup: 'no',
    selectedList: [

    ],
  },
  onLoad() {
    this.disposeData()


    // console.log(this.$store.data);
    // let data = {
    //   pmPeriodCode: ''
    // }
    // getRouteForm(data).then(rs => {
    //   console.log(rs);
    // })
  },
  // 提示未选择考评表弹窗关闭
  modelClose() {
    this.setData({
      isShowInputValueModel: false
    })
  },

  // 从store里面取出数据，对数据进行处理的函数
  disposeData() {
    // let newArr = this.$store.data.selectedList
    // console.log(newArr);
  },
  // 按考评组的单选框选中事件函数
  changeGroup(checked, e) {

    // console.log(checked, '选中状态');
    if (this.data.inputVal == '请选择') {
      this.setData({
        isShowInputValueModel: true
      })
      return
    }

    let { id } = e.currentTarget.dataset
    if (this.data.group == false) {
      this.setData({
        group: checked,
        user: !checked
      })
    }
    // if (this.$store.data.userOrGroup == 'user') {
    //   this.$store.data.selectedList = []
    //   this.update()
    // }
    dd.navigateTo({
      url: `/pages/manage/mark/groupList/groupList?pmFormId=${this.data.pmFormId}`
    })
    // console.log(checked, this.data.group, 'group');
  },

  // 判断是考评组还是员工的函数
  userOrGroupFunc() {
    let isUser = null;
    if (this.$store.data.userOrGroup == 'user') {
      isUser = true
    } else if (this.$store.data.userOrGroup == 'group') {
      isUser = false
    } else {
      isUser = '暂未选择'
    }
    // console.log(isUser);
    return isUser
  },

  // 按员工的单选框选中事件函数
  changeUser(checked, e) {
    if (this.data.inputVal == '请选择') {
      this.setData({
        isShowInputValueModel: true
      })
      return
    }
    // console.log(checked, e);
    if (this.data.user == false) {
      this.setData({
        user: checked,
        group: !checked
      })
    }

    dd.navigateTo({
      url: `/pages/manage/mark/user/user?pmFormId=${this.data.pmFormId}`
    })
    // console.log(checked, this.data.user, 'user');

    // 将已选的数据更新到store
    this.$store.data.selectedList = this.data.selectedList
    this.update()

    this.$store.data.userOrGroup = 'user'
    this.update()
  },
  selectionForm() {
    dd.navigateTo({
      url: '/pages/manage/mark/pmForm/pmForm'
    })
  },
  changeData(value, id) {
    this.setData({
      inputVal: value,
      pmFormId: id
    }),
      this.$store.data.pmFormId = id
    this.update()
  },
  changeFormGroup(value) {
    this.setData({
      pmGroupName: value
    })
  },
  // 确认
  handleTap() {
    console.log(this.data);
    // console.log(this.data.pmFormId);
    if (!this.data.group && !this.data.user) {
      dd.alert({
        title: '提示',
        content: '请在考评组和员工之间二选一',
        buttonText: '我知道了',
      })
      // console.log('请在考评组和员工之间二选一');
    } else {
      // 判断有没有选择考评组和员工
      if (this.data.selectedList.length == 0) {
        dd.alert({
          title: '提示',
          content: '请选择考评组或员工',
          buttonText: '我知道了',
        })
      } else {
        let gil = []
        let udl = []
        if (this.data.group && !this.data.user) {
          this.data.selectedList.forEach((gItem) => {
            gil.push(gItem.pmGroupId)
          })
        } else if (!this.data.group && this.data.user) {
          this.data.selectedList.forEach((uItem) => {
            udl.push(uItem.userDDId)
          })
        }
        dd.getStorage({
          key: 'currentCity',
          success: (res) => {
            console.log('登录信息', res);
            let data = {
              pmFormId: this.data.pmFormId,
              pmGroupIdList: gil.join(';'),
              userDDList: udl.join(';'),
              startUserDDId: res.data.value.userId
            }
            console.log(data);

            startScore(data).then((res) => {
              console.log('发起评分成功', res);
              dd.showToast({
                content: "发起成功",
                type: "success",
                duration: 1000
              })
            })

          },
          fail: function (res) {

          }
        });


      }

    }
    // console.log('确认', this.data.group, this.data.user);
  },
  // 重置
  reset() {
    this.setData({
      inputVal: '请选择',
      group: false,
      user: false,
    })
    this.$store.data.selectedList = []
    this.update()
    // console.log('重置', this.data.group, this.data.user);
  },
  // 已选列表的删除按钮点击事件
  iconDel(e) {
    let index = e.currentTarget.dataset.index
    // console.log(index);
    let newArr = this.$store.data.selectedList
    newArr.splice(index, 1)
    // console.log(newArr);
    this.setData({
      selectedList: newArr
    })
    this.$store.data.selectedList = this.data.selectedList
    this.update()
  },

});
